﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using WebScrapper.Entities;
using App.Core.Extensions;
using WebScrapper.Helpers;
using System.Net;
using WebScrapper.Implementations;
using WebScrapper.Implementations.ZukaBuka;

namespace WebScrapper
{   
    public partial class frmMain : Form
    {        
        private const String DOMAIN = "http://www.pkt.pl";
        public frmMain()
        {
            InitializeComponent();
            AppGlobal.CurrentForm = this;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            // Start the asynchronous operation.
            if (backgroundWorker1.IsBusy != true)
                backgroundWorker1.RunWorkerAsync(this);
            
            //string cookie = ConfigReader.AuthenticationCookieValue; //@"ASP.NET_SessionId=owaiwkzc2e0cye55i4yvme55; .MCAIDASPXFORMS=3AFFCC066B847E986E61BCE7A89B1AC6425069EB95D1B7060C473218AB2644BDA6FA2ADDB1690E435F016F641525E9E2A896A3C9AA1A73CD9A85547CAA536A9E5A8690CBDBBECEA9562C160DAFC41F39433877FD66E60738DBBFC4429ADBA4D21C4E31EA1BA9A08E95271275844BD01BF475A769232C4604A2074834FF458D4D810E051C92BB6C8D782B347F204A0187730A48A5";
            //string url = "https://community.rallydev.com/slm/rally.sp";
            //string html = AppUtil.GetAuthenticatedPageContent(url, cookie);

            #region Test Codes for CymaxWeb
            //HtmlWeb hw = new HtmlWeb();
            ////HtmlAgilityPack.HtmlDocument doc = hw.Load("http://www.cymaxstores.com/Trademark-Global-Savoy-House-Exterior-Wall-Lantern-87-5001.htm");
            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.Load(@"C:\Users\Mimi\Desktop\New folder (2)\CymexProductDetails.html");
            ////doc.Load(html);
            //if (doc != null)
            //{
            //    HtmlNode detailsNode = doc.DocumentNode.SelectSingleNode("//div[@class='details-box']");
            //    HtmlAgilityPack.HtmlDocument xmlToSave = new HtmlAgilityPack.HtmlDocument();
            //    xmlToSave.LoadHtml(detailsNode.WriteContentTo());
            //    xmlToSave.OptionOutputAsXml = true;
            //    String filePath = "NewDetails.xml";
            //    xmlToSave.Save(filePath);



            //    if (System.IO.File.Exists(filePath))
            //    {
            //        System.IO.StreamReader stream = new System.IO.StreamReader(filePath);
            //        System.Xml.Serialization.XmlSerializer xmlSerilizer = new System.Xml.Serialization.XmlSerializer(typeof(CymaxWeb.span));
            //        CymaxWeb.span productDetails = xmlSerilizer.Deserialize(stream) as CymaxWeb.span;
            //        //return updateInfo;
            //    }
            //}
            #endregion Test Codes for CymaxWeb

            //WebScrapper.Implementations.CymaxWeb cymaxWeb = new WebScrapper.Implementations.CymaxWeb(this);
            //cymaxWeb.StartScrapping(txtCategory.Text.Trim());
        }        
        private void btnStop_Click(object sender, EventArgs e)
        {            
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker1.CancelAsync();
            }
        } 
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {           
            BackgroundWorker worker = sender as BackgroundWorker;
            frmMain form = e.Argument as frmMain;
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                form.btnStart.Enabled = true;                
                return;
            }
            else
            {   
                //worker.ReportProgress(i * 10);
                
                ///Start Scrapping
                //PolskaImplementation scrapper = new PolskaImplementation(form.txtStatus);                                
                //CymaxWeb scrapper = new CymaxWeb(form.txtStatus);                
                //JuvedermUltra scrapper = new JuvedermUltra(form.txtStatus);
                //Baaps scrapper = new Baaps(form.txtStatus);
                //WebScrapper.Implementations.Alice.Manta scrapper = new Implementations.Alice.Manta(form.txtStatus);
                _411CA scrapper = new _411CA(form.txtStatus);            
                scrapper.StartScrapping(form.txtCategory.Text.Trim());               
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //while (true)
            //{
            //}
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.btnStart.Enabled = true;

            if (e.Cancelled == true)
                MessageBox.Show("Operation Stopped!", AppConstants.PRODUCT_NAME, MessageBoxButtons.OK, MessageBoxIcon.Information);
            else if (e.Error != null)
                MessageBox.Show(String.Format("Following Error Occured: {0} {1}", Environment.NewLine, e.Error.Message), AppConstants.PRODUCT_NAME, MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("All Data were Scrapped Successfully.", AppConstants.PRODUCT_NAME, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }        
    }
}
